<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8" />
        <title>chapter8-1</title>
        <link rel="stylesheet" href="/theme/css/main.css" />

        <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
</head>

<body id="index" class="home">
        <header id="banner" class="body">
                <h1><a href="/">PythonTutorials </a></h1>
                <nav><ul>
                    <li class="active"><a href="/category/cpp.html">cpp</a></li>
                    <li><a href="/category/python.html">python</a></li>
                </ul>
                </nav>
        </header><!-- /#banner -->
<section id="content" class="body">
  <article>
    <header>
      <h1 class="entry-title">
        <a href="/chapter8-1.html" rel="bookmark"
           title="Permalink to chapter8-1">chapter8-1</a></h1>
    </header>

    <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/cpp.html">cpp</a><a href="/tag/progarmming.html">progarmming</a></span>
</footer><!-- /.post-info -->      <h2>一、 指针变量定义</h2>
<p>C语言有两种常量：
* 值常量
* 符号常量</p>
<p>C语言有两种变量：
* 内容变量——普通变量
* 地址变量——指针变量</p>
<h1>指针变量的定义：类型名    * 指针变量名</h1>
<ul>
<li>int a,b,<em>p1,</em>p2;</li>
<li>float a,b,<em>p3,</em>p4;</li>
<li>char s,<em>p5,</em>p6;</li>
</ul>
<h3><strong>==注意事项==</strong></h3>
<ul>
<li>定义变量（指针变量、普通变量）都必须在前面添加类型名</li>
<li>定义指针变量时，指针变量前面的“*”表示定义的是一个指针类型的变量，星号不是指针变量的一部分，只是一个标志。</li>
<li>指针变量专门用来存储地址，禁止将一个整型直接赋值给一个指针变量。</li>
<li>内容变量存内容，地址变量存地址</li>
</ul>
<h1>指针变量的引用</h1>
<ul>
<li>“*”为指针运算符，可以取出指针变量指向的普通变量的值（间接引用普通变量）</li>
<li>“&amp;”为地址运算符，通过&amp;可以取出普通变量中的地址（单目，二级，从右向左运算）</li>
</ul>
<h2><strong>==注意事项==</strong></h2>
<ul>
<li>(1)可以通过赋值使一个指针变量“指向”某一普通变量(指针变量=&amp;普通变量),比如：int a=10;int *p;p=&a;</li>
<li>(2)先让指针变量指向一个确定的存储单元之后，再通过该指针变量引用它所指向的存储单(定义且初始化之后方可用，不可以随便指向普通变量或常量)</li>
<li>(3)变量名(普通变量和指针变量)都表示存储单元的值。int <em>p1,</em>p2;p1=p2;<em>p1(写)=</em>p2(读);后面语句均合法。</li>
<li>==<strong>内容变量画房子，指针变量画指向</strong>==</li>
<li>(4)若指针变量p指向普通变量a，即将变量a的地址赋给了指针变量p。</li>
<li>如：int a =10,*p=&a;则有如下结果（做题时，可以等价替换）：<ul>
<li>*p&lt;=&gt;a</li>
<li>p&lt;=&gt;&amp;a</li>
<li>&amp;*p&lt;=&gt;&amp;a&lt;=&gt;p</li>
<li><em>&amp;a &lt;=&gt;</em>p &lt;=&gt;a</li>
<li>(*p)++ a++</li>
<li>(*p)-- a--</li>
<li>--(<em>p) --a --</em>p(单目，二级，结合方向从右向左)</li>
<li>++(<em>p) ++a ++</em>p</li>
<li>==注：上述&lt;=&gt;为等价于，为等价表==</li>
</ul>
</li>
<li>(5)所有指针变量在内存中分配的字节数(均为2位)都是相同的，sizeof();int <em>p1;double </em>p2;float *p3;三个的sizeof相同，该类型指的是普通变量的内容，同类型互指。</li>
</ul>
<h1>“*”的三角色</h1>
<ul>
<li>乘法运算符(双目)</li>
<li>指针运算符(2级，单目，从右向左)</li>
<li>指针定义的标志，该*不是变量的一部分，是指针变量的标志</li>
<li></li>
</ul>
<h1>口诀：</h1>
<h2>1. 计算表达式</h2>
<h2>2. 分析程序有多少条语句，结构的</h2>
<h2>3. 复合框架执行过程</h2>
<h2>4. 指针（1.地址变量得地址， 指向谁得到谁的地址； 2.有*是内容值，不是读就是写，在赋值号左边是写，在赋值号右边是读3.无*改变量指向的地址4.(指针变量必须定义且初始化之后方可用）</h2>
<h1>指针例题分析</h1>
<h3>例一</h3>
<div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">fun</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">);</span><span class="c1">//原型声明，参数类型为指针变量</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> 
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
    <span class="n">fun</span><span class="p">(</span><span class="o">&amp;</span><span class="n">y</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d%d&quot;</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span><span class="c1">//打印普通变量</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">fun</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">)</span> <span class="c1">//形参为指针变量</span>
<span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d%d&quot;</span><span class="p">,</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="o">*</span><span class="n">y</span><span class="p">);</span> <span class="c1">// 打印形参指针所指向的普通变量的值</span>
    <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span> <span class="c1">//更改指针变量所指向的普通变量的内容值，下同</span>
    <span class="o">*</span><span class="n">y</span> <span class="o">=</span> <span class="mi">4</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<h4>例二（画图，口诀）</h4>
<div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">swap</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">p1</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">p2</span><span class="p">);</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">;</span>
    <span class="kt">int</span> <span class="o">*</span><span class="n">p1</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="n">p2</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;</span>
    <span class="n">scanf</span><span class="p">(</span><span class="s">&quot;%d%d&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">);</span>
    <span class="n">swap</span><span class="p">(</span><span class="n">p1</span><span class="p">,</span><span class="n">p2</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d,%d&quot;</span><span class="p">,</span><span class="o">*</span><span class="n">p1</span><span class="p">,</span><span class="o">*</span><span class="n">p2</span><span class="p">);</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">swap</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">p1</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">p2</span><span class="p">)</span> 
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">temp</span><span class="p">;</span>
    <span class="n">temp</span> <span class="o">=</span> <span class="o">*</span><span class="n">p1</span><span class="p">;</span>
    <span class="o">*</span><span class="n">p1</span> <span class="o">=</span> <span class="o">*</span><span class="n">p2</span><span class="p">;</span>
    <span class="o">*</span><span class="n">p2</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<h3>例三（画图，应用指针口诀，改变指针指向）</h3>
<div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">swap2</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">p1</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">p2</span><span class="p">);</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">;</span>
    <span class="kt">int</span> <span class="o">*</span><span class="n">p1</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="n">p2</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;</span>
    <span class="n">scanf</span><span class="p">(</span><span class="s">&quot;%d%d&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">);</span>
    <span class="n">swap2</span><span class="p">(</span><span class="n">p1</span><span class="p">,</span><span class="n">p2</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d,%d&quot;</span><span class="p">,</span><span class="o">*</span><span class="n">p1</span><span class="p">,</span><span class="o">*</span><span class="n">p2</span><span class="p">);</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">swap2</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">p1</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">p2</span><span class="p">)</span> <span class="c1">//只会交换局部指针变量（形参）所指向的地址</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="o">*</span><span class="n">temp</span><span class="p">;</span>
    <span class="n">temp</span> <span class="o">=</span> <span class="n">p1</span><span class="p">;</span>
    <span class="n">p1</span> <span class="o">=</span> <span class="n">p2</span><span class="p">;</span>
    <span class="n">p2</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d,%d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="o">*</span><span class="n">p1</span><span class="p">,</span> <span class="o">*</span><span class="n">p2</span><span class="p">);</span> <span class="c1">//外部指针变量的值没有变化</span>
<span class="p">}</span>
</pre></div>
    </div><!-- /.entry-content -->

  </article>
</section>
        <section id="extras" class="body">
                <div class="blogroll">
                        <h2>blogroll</h2>
                        <ul>
                            <li><a href="http://getpelican.com/">Pelican</a></li>
                            <li><a href="http://python.org/">Python.org</a></li>
                            <li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
                            <li><a href="#">You can modify those links in your config file</a></li>
                        </ul>
                </div><!-- /.blogroll -->
                <div class="social">
                        <h2>social</h2>
                        <ul>

                            <li><a href="#">You can add links in your config file</a></li>
                            <li><a href="#">Another social link</a></li>
                        </ul>
                </div><!-- /.social -->
        </section><!-- /#extras -->

        <footer id="contentinfo" class="body">
                <p>Powered by <a href="http://www.maxiaoan.com/">Maxiaoan</a></p>
        </footer><!-- /#contentinfo -->

</body>
</html>